<?php require_once('../Connections/sepolwan_connect.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
include "../txt/url.php";
include "../tambah/date.php";
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_sesi_member = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_sesi_member = $_SESSION['MM_Username'];
}
mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_sesi_member = sprintf("SELECT * FROM memberdb WHERE email = %s", GetSQLValueString($colname_sesi_member, "text"));
$sesi_member = mysql_query($query_sesi_member, $sepolwan_connect) or die(mysql_error());
$row_sesi_member = mysql_fetch_assoc($sesi_member);
$totalRows_sesi_member = mysql_num_rows($sesi_member);
if ((isset($_POST["pc"])) && ($_POST["pc"] <> "")) {
	$insertSQL = sprintf("INSERT INTO chat (id, dari, ke, pesan, stat, waktu) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString("", "int"),
                       GetSQLValueString($row_sesi_member['nama_user'], "text"),
					   GetSQLValueString($_GET['user'], "text"),
					   GetSQLValueString(str_replace(array('<', '>'), array('&lt;', '&gt;'), $_POST['pc']), "text"),
					   GetSQLValueString('ur', "text"),
					   GetSQLValueString(date('Y-m-d H:i:s'), "text"));

  mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
  $Result1 = mysql_query($insertSQL, $sepolwan_connect) or die(mysql_error());
  $updateGoTo = $_GET['user'].".html";
  header(sprintf("Location: %s", $updateGoTo));
}
$colname_get = "-1";
if (isset($_GET['user'])) {
  $colname_get = $_GET['user'];
}
mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_get = sprintf("SELECT * FROM memberdb WHERE nama_user = %s", GetSQLValueString($colname_get, "text"));
$get = mysql_query($query_get, $sepolwan_connect) or die(mysql_error());
$row_get = mysql_fetch_assoc($get);
$totalRows_get = mysql_num_rows($get);
if ($totalRows_get >0) {
mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_ikut_me = "SELECT * FROM ikut WHERE dari = '".$row_sesi_member['id']."' AND  ke = '".$row_get['id']."'  ORDER BY id DESC";
$ikut_me = mysql_query($query_ikut_me, $sepolwan_connect) or die(mysql_error());
$row_ikut_me = mysql_fetch_assoc($ikut_me);
$totalRows_ikut_me = mysql_num_rows($ikut_me);
?>


<?php if ($totalRows_ikut_me > 0) { // Show if recordset not empty ?>
  <?php 
  mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_ikut_ = "SELECT * FROM ikut WHERE dari = '".$row_get['id']."' AND ke = '".$row_sesi_member['id']."' ORDER BY id DESC";
$ikut_ = mysql_query($query_ikut_, $sepolwan_connect) or die(mysql_error());
$row_ikut_ = mysql_fetch_assoc($ikut_);
$totalRows_ikut_ = mysql_num_rows($ikut_);
  ?>
  <?php if ($totalRows_ikut_ > 0) { // Show if recordset not empty 
  mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_chat = "SELECT * FROM chat WHERE dari = '".$_GET['user']."' and ke = '".$row_sesi_member['nama_user']."' or ke = '".$_GET['user']."' and dari = '".$row_sesi_member['nama_user']."' ORDER BY id ASC ";
$chat = mysql_query($query_chat, $sepolwan_connect) or die(mysql_error());
$row_chat = mysql_fetch_assoc($chat);
$totalRows_chat = mysql_num_rows($chat);

$updateSQL = sprintf("UPDATE chat SET stat=%s WHERE dari=%s and ke=%s",
                       
					   GetSQLValueString('re', "text"),
                       GetSQLValueString($_GET['user'], "text"),
                       GetSQLValueString($row_sesi_member['nama_user'], "text"));
					   
   mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
  $Result1 = mysql_query($updateSQL, $sepolwan_connect) or die(mysql_error());
  ?>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $_GET['user'] ?></title>
<link href='<?php echo $base ?>images/log.png' rel='shortcut icon'/>
<link href="<?php echo $base ?>gaya/style.css" rel="stylesheet" type="text/css" />
</head>
<script language="javascript" src="<?php echo $base ?>pesan/jquery-1.2.6.min.js"></script>
<script language="javascript" src="<?php echo $base ?>pesan/jquery.timers-1.0.0.js"></script>
<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
	j(document).ready(function()
	{
		j(".refresh").everyTime(1000,function(i){
			j.ajax({
			  url: "<?php echo $base ?>refresh/<?php echo $_GET['user'] ?>.html",
			  cache: false,
			  success: function(html){
				j(".refresh").html(html);
			  }
			})
		})
		
	});
	 j('.refresh').css({color:"green"});
});
$(".refresh").scrollTop($(".refresh")[0].scrollHeight);
</script>
<script  type="text/javascript">
window.onload=toBottom;

function toBottom()
{
window.scrollTo(0, document.body.scrollHeight);
}
</script>

<body>
<div style="width:100%; background:#000; color:#FFF; position:fixed; top:0;"><div style=" padding:10px;"><a href="index.php" style="color:#FFF;">&lt; Back</a> | @<?php echo $_GET['user'] ?></div></div>
<div style="height:50px"></div>
  <?php do { ?>
  <?php if ($row_chat['dari']==$_GET['user']) { ?>
    <div style="margin:10px; min-height:30px; "><div style=" float:left"><img src="<?php  if(file_exists("../images/member/ava/".md5($row_get['id']).".jpg")) { echo $base."images/member/ava/tester_30_30_".md5($row_get['id']).".jpg"; } else { echo $base."images/member/ava/tester_30_30_default.jpg"; }  ?>"  /></div>
    <div style="margin-left:35px; margin-top:3px; background:#FFF; -moz-box-shadow:    1px 1px 1px #666;
  -webkit-box-shadow: 1px 1px 1px #666;
  box-shadow:         1px 1px 1px #666; padding:5px">
<font size="-2"><?php echo $row_chat['pesan']; ?></font><br />
<font size="-4" color="#999999"><?php echo tgl_indo($row_chat['waktu']), substr($row_chat['waktu'], 10, 6), " WIB"; ?></font></div></div>
<?php } ?>
<?php if ($row_chat['dari']==$row_sesi_member['nama_user']) { ?>
    <div style="margin:10px; min-height:30px; "><div style="width:30px; height:30px; overflow:hidden;  float:right"><img src="<?php  if(file_exists("../images/member/ava/".md5($row_sesi_member['id']).".jpg")) { echo $base."images/member/ava/tester_30_30_".md5($row_sesi_member['id']).".jpg"; } else { echo $base."images/member/ava/tester_30_30_default.jpg"; }  ?>" /></div>
    <div style="margin-right:35px; margin-top:3px; background: #E9F7FE; -moz-box-shadow:    1px 1px 1px #666;
  -webkit-box-shadow: 1px 1px 1px #666;
  box-shadow:         1px 1px 1px #666; padding:5px; text-align:right">
<font size="-2"><?php echo $row_chat['pesan']; ?></font><br />
<font size="-4" color="#999999"><?php echo tgl_indo($row_chat['waktu']), substr($row_chat['waktu'], 10, 6), " WIB"; ?> | <a href="<?php echo $base."delete-".$row_chat['id']."-".$row_sesi_member['nama_user']."/".$_GET['user'].".html" ?>">delete</a></font></div></div>
<?php } ?>
    <?php } while ($row_chat = mysql_fetch_assoc($chat)); ?><div class="refresh"></div><div style="height:80px"></div>
    <div align="center" style="background:#000; position:fixed; bottom:0; width:100%"><form action="<?php echo $_GET['user'] ?>.html" method="post" name="chat">
    <input name="pc" type="text" value="" placeholder="press enter to sent"  style="width:95%" autofocus="autofocus" />
    </form></div>
    </body>
</html>
<?php 
mysql_free_result($chat); } else { echo "<div align='center'>Pengiriman Pesan dapat dilakukan jika anda dengan ".$_GET['user']." saling mengikuti (follow)</div>"; }?>
  <?php  mysql_free_result($ikut_); } // Show if recordset not empty 
  else { echo "<div align='center'>Pengiriman Pesan dapat dilakukan jika anda dengan ".$_GET['user']." saling mengikuti (follow)</div>"; }?>

<?php

mysql_free_result($ikut_me);
}
mysql_free_result($sesi_member);
mysql_free_result($get);

?>
